//
//  XGardenNewsViewModel.swift
//  WeiBoReDo
//
//  Created by 放学跟鸽走 on 2017/4/15.
//  Copyright © 2017年 放学跟鸽走. All rights reserved.
//

import Foundation
/// 园地新闻视图模型
class XGardenNewsViewModel {
    /// 鸽园新闻
    var gardenNews:XGardenNews?
    
//    /// 配图
//    var img:UIImage?
//    
//    /// 标题
//    var title:String?
//    
//    /// 作者
//    var author:String?
//    
//    /// 评论
//    var comment:String?
    
    /// 行高
    var rowHeight: CGFloat = 85
    ///根据类型返回不同的结果
    var picURLs:[String]?{
        let type = gardenNews?.cellType ?? .Normal
        switch type {
        case .BigPic:
            return [gardenNews?.img1 ?? ""]
        case .ThreePic:
            return gardenNews?.imgList
        default:
            return [gardenNews?.img ?? ""]
        }
    }
    /// 配图视图的大小
    var picViewSize:CGSize =  CGSize()
    /// 初始化新闻视图模型
    ///
    /// - Parameter model: XGardenNews
    init(model:XGardenNews){
        self.gardenNews = model
        
        // 计算行高
        updateRowHeight()
    }
     // 计算行高
    func updateRowHeight(){
        
        let margin:CGFloat = 10//边距
        //图文样式：1：代表一个配图 2：代表3个配图 3：大图
        switch gardenNews?.cellType ?? .Normal {
        case XGardenNewsCellType.Normal:
            //normal:上边距（10）+配图高(65)+下边距(10)+分隔线(0.5)
            let imgH:CGFloat = 65//配图高度
            rowHeight = margin+imgH+margin+0.5
        case XGardenNewsCellType.BigPic:
            //big:上边距(10) + 标题文字高度(计算)+边距（10)+图片高度(计算)+边距（10）+ 作者文字高度（计算）+ 下边距(10)+分隔线(0.5)
            let title = gardenNews?.title as NSString?
            let titleSize = title?.size(attributes: [NSFontAttributeName : UIFont.systemFont(ofSize: 14)])
            let titleH = titleSize?.height ?? 0
            let imgH:CGFloat = (UIScreen.cz_screenWidth() - 2*margin)/2
            let author = gardenNews?.author as NSString?
            let authSize = author?.size(attributes: [NSFontAttributeName : UIFont.systemFont(ofSize: 12)])
            let authH = authSize?.height ?? 0
            rowHeight = margin+titleH+margin+imgH+margin+authH+margin+0.5
        case XGardenNewsCellType.ThreePic:
            //big:上边距(10) + 标题文字高度(计算)+边距（10+图片高度(计算)+边距（10）+ 作者文字高度（计算）+ 下边距(10)+分隔线(0.5)
            let title = gardenNews?.title as NSString?
            let titleSize = title?.size(attributes: [NSFontAttributeName : UIFont.systemFont(ofSize: 14)])
            let titleH = titleSize?.height ?? 0
            let imgH:CGFloat = 65
            let font = UIFont.systemFont(ofSize: 12)
            let lineH = font.lineHeight
            rowHeight = margin+titleH+margin+imgH+margin+lineH+margin+0.5
        }
        
      
    
    }
}
